package com.jsle.financial.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.jsle.common.dao.mybatis.BaseDao;
import com.jsle.financial.domain.User;
import com.jsle.financial.domain.User_UserGroups;
import com.jsle.financial.util.Constants;
import com.jsle.financial.vo.Page;
import com.jsle.financial.vo.PageAndParameters;
import com.jsle.financial.vo.Result;


@SuppressWarnings("unchecked")
public class UserDaoImpl extends BaseDao implements UserDao{
	private Log log = LogFactory.getLog(UserDaoImpl.class);
	/**
	 * 新增一用户
	 * @param user
	 * @return
	 */
	public Result addUser(User user){
		Result result = new Result();
		try {
			boolean ret = insert("User.insertUser", user);
			if(ret){
				result.setResultCode(Constants.FC_RESULT_SUCCESS_CODE);
				result.setResultMessage(Constants.FC_RESULT_SUCCESS_MESSAGE);
			}else{
				result.setResultCode(Constants.Dao.FC_RESULT_INSERT_DAO_ERROR_CODE);
				result.setResultMessage(String.format(Constants.Dao.FC_RESULT_INSERT_DAO_ERROR_MESSAGE, "User"));
			}
		} catch (Exception e) {
			result.setResultCode(Constants.Dao.FC_RESULT_INSERT_DAO_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Dao.FC_RESULT_INSERT_DAO_ERROR_MESSAGE, "User"));
			log.error("new user error## "+ e.getMessage());
		}
		return result;
	}
	/**
	 * 删除一用户
	 * @param user
	 * @return
	 */
	public Result delUsers(User user){
		Result result = new Result();
		try {
			boolean ret = delete("User.delUser",user);
			if(ret){
				result.setResultCode(Constants.FC_RESULT_SUCCESS_CODE);
				result.setResultMessage(Constants.FC_RESULT_SUCCESS_MESSAGE);
			}else{
				result.setResultCode(Constants.Dao.FC_RESULT_DEL_DAO_ERROR_CODE);
				result.setResultMessage(String.format(Constants.Dao.FC_RESULT_DEL_DAO_ERROR_MESSAGE, "User"));
			}
		} catch (Exception e) {
			result.setResultCode(Constants.Dao.FC_RESULT_DEL_DAO_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Dao.FC_RESULT_DEL_DAO_ERROR_MESSAGE, "User"));
			log.error("del user error## "+ e.getMessage());
		}
		return result;
	}
	/**
	 * 更新一用户信息
	 * @param user
	 * @return
	 */
	public Result updateUser(User user){
		Result result = new Result();
		try {
			boolean ret = update("User.updateUser",user);
			if(ret){
				result.setResultCode(Constants.FC_RESULT_SUCCESS_CODE);
				result.setResultMessage(Constants.FC_RESULT_SUCCESS_MESSAGE);
			}else{
				result.setResultCode(Constants.Dao.FC_RESULT_UPDATE_DAO_ERROR_CODE);
				result.setResultMessage(String.format(Constants.Dao.FC_RESULT_UPDATE_DAO_ERROR_MESSAGE, "User"));
			}
		} catch (Exception e) {
			result.setResultCode(Constants.Dao.FC_RESULT_UPDATE_DAO_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Dao.FC_RESULT_UPDATE_DAO_ERROR_MESSAGE, "User"));
			log.error("update user error## "+ e.getMessage());
		}
		return result;
	}
	/**
	 * 根据登录账号获取一用户信息
	 * @param loginName
	 * @return
	 */
	public Result queryUserByLoginName(User userParameter){
		Result result = new Result();
		try {
			User user = (User)queryCountForObject("User.selUser",userParameter);
			if(user!=null){
				result.setResultCode(Constants.FC_RESULT_SUCCESS_CODE);
				result.setResultMessage(Constants.FC_RESULT_SUCCESS_MESSAGE);
				result.setResultValue(user);
			}else{
				result.setResultCode(Constants.Dao.FC_RESULT_QUERY_DAO_ERROR_CODE);
				result.setResultMessage(String.format(Constants.Dao.FC_RESULT_QUERY_DAO_ERROR_MESSAGE, "User"));
			}
		} catch (Exception e) {
			result.setResultCode(Constants.Dao.FC_RESULT_QUERY_DAO_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Dao.FC_RESULT_QUERY_DAO_ERROR_MESSAGE, "User"));
			log.error("query user error## "+ e.getMessage());
		}
		return result;
	}
	
	/**
	 * 分页查询
	 * @param page
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Result queryUsersByPage(Page page,String loginName){
		Result result = new Result();
		PageAndParameters param = new PageAndParameters(page.getPageIndex(),page.getPageSize(),loginName);
		try {
			List<User> users = queryForList("User.selUsers", param);
			
			User user = new User(loginName);
			int totalCount = (Integer)queryCountForObject("User.selUsersCounts", user);
			if (totalCount < 1){
				result.setResultCode(Constants.FC_RESULT_SUCCESS_CODE);
				result.setResultMessage(Constants.FC_RESULT_SUCCESS_MESSAGE);
				result.setResultValue(new Page());
				return result;
			}
				

			int startIndex = Page.getStartOfPage(page.getPageIndex(), page.getPageSize());
			
			if(users!=null){
				result.setResultCode(Constants.FC_RESULT_SUCCESS_CODE);
				result.setResultMessage(Constants.FC_RESULT_SUCCESS_MESSAGE);
				result.setResultValue(new Page(startIndex, totalCount, page.getPageSize(), users));
			}else{
				result.setResultCode(Constants.Dao.FC_RESULT_QUERY_DAO_ERROR_CODE);
				result.setResultMessage(String.format(Constants.Dao.FC_RESULT_QUERY_DAO_ERROR_MESSAGE, "UsersByPage"));
			}
		} catch (Exception e) {
			result.setResultCode(Constants.Dao.FC_RESULT_QUERY_DAO_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Dao.FC_RESULT_QUERY_DAO_ERROR_MESSAGE, "UsersByPage"));
			log.error("query users by page error## "+ e.getMessage());
		}
		return result;
	}
	
	/**
	 * 用户配置用户组
	 * @param uug
	 * @return
	 */
	public Result addUserAndUserGroups(User_UserGroups uug) {
		Result result = new Result();
		try {
			boolean ret = insert("UserUserGroup.insertUserUserGroup",uug);
			if(ret){
				result.setResultCode(Constants.FC_RESULT_SUCCESS_CODE);
				result.setResultMessage(Constants.FC_RESULT_SUCCESS_MESSAGE);
			}else{
				result.setResultCode(Constants.Dao.FC_RESULT_UPDATE_DAO_ERROR_CODE);
				result.setResultMessage(String.format(Constants.Dao.FC_RESULT_UPDATE_DAO_ERROR_MESSAGE, "User"));
			}
		} catch (Exception e) {
			result.setResultCode(Constants.Dao.FC_RESULT_UPDATE_DAO_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Dao.FC_RESULT_UPDATE_DAO_ERROR_MESSAGE, "User"));
			log.error("add User_UserGroups error## "+ e.getMessage());
		}
		return result;
	}
	
	/**
	 * 删除用户配置用户组
	 * @param uug
	 * @return
	 */
	public Result delUserAndUserGroups(User_UserGroups uug) {
		Result result = new Result();
		try {
			boolean ret = insert("UserUserGroup.delUserUserGroup",uug);
			if(ret){
				result.setResultCode(Constants.FC_RESULT_SUCCESS_CODE);
				result.setResultMessage(Constants.FC_RESULT_SUCCESS_MESSAGE);
			}else{
				result.setResultCode(Constants.Dao.FC_RESULT_UPDATE_DAO_ERROR_CODE);
				result.setResultMessage(String.format(Constants.Dao.FC_RESULT_UPDATE_DAO_ERROR_MESSAGE, "User_UserGroups"));
			}
		} catch (Exception e) {
			result.setResultCode(Constants.Dao.FC_RESULT_UPDATE_DAO_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Dao.FC_RESULT_UPDATE_DAO_ERROR_MESSAGE, "User_UserGroups"));
			log.error("del User_UserGroups error## "+ e.getMessage());
		}
		return result;
	}
	
	/**
	 * 查询某个用户配置的用户组
	 * @param uug
	 * @return
	 */
	public Result selUserAndUserGroups(User_UserGroups uug){
		Result result = new Result();
		try {
			List<User_UserGroups> uugs = queryForList("UserUserGroup.selUserUserGroups",uug);
			if(uugs!=null){
				result.setResultCode(Constants.FC_RESULT_SUCCESS_CODE);
				result.setResultMessage(Constants.FC_RESULT_SUCCESS_MESSAGE);
				result.setResultValue(uugs);
			}else{
				result.setResultCode(Constants.Dao.FC_RESULT_QUERY_DAO_ERROR_CODE);
				result.setResultMessage(String.format(Constants.Dao.FC_RESULT_QUERY_DAO_ERROR_MESSAGE, "UsersByPage"));
			}
		} catch (Exception e) {
			result.setResultCode(Constants.Dao.FC_RESULT_UPDATE_DAO_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Dao.FC_RESULT_UPDATE_DAO_ERROR_MESSAGE, "User_UserGroups"));
			log.error("selUserAndUserGroups error## "+ e.getMessage());
		}
		return result;
	}

	/**
	 * 根据申请人获取流程审核人员列表
	 * @param user
	 * @author wisdying
	 * @return
	 */
	public Result queryAssessors(User user){
		Result result = new Result();
		try {
			List<User> assessors = (List<User>)queryForList("User.selAssessors",user);
			if(user!=null){
				result.setResultCode(Constants.FC_RESULT_SUCCESS_CODE);
				result.setResultMessage(Constants.FC_RESULT_SUCCESS_MESSAGE);
				result.setResultValue(assessors);
			}else{
				result.setResultCode(Constants.Dao.FC_RESULT_QUERY_DAO_ERROR_CODE);
				result.setResultMessage(String.format(Constants.Dao.FC_RESULT_QUERY_DAO_ERROR_MESSAGE, "User"));
			}
		} catch (Exception e) {
			result.setResultCode(Constants.Dao.FC_RESULT_QUERY_DAO_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Dao.FC_RESULT_QUERY_DAO_ERROR_MESSAGE, "User"));
			log.error("queryAssessors error## "+ e.getMessage());
		}
		return result;
	}
	
	/**
	 * 根据Id获取用户信息
	 * @param userId
	 * @author wisdying
	 * @return
	 */
	public Result queryUserById(Integer userId){
		Result result = new Result();
		try {
			User userx = new User(userId);
			User user = (User)queryForObject("User.selUserById",userx);
			if(user!=null){
				result.setResultCode(Constants.FC_RESULT_SUCCESS_CODE);
				result.setResultMessage(Constants.FC_RESULT_SUCCESS_MESSAGE);
				result.setResultValue(user);
			}else{
				result.setResultCode(Constants.Dao.FC_RESULT_QUERY_DAO_ERROR_CODE);
				result.setResultMessage(String.format(Constants.Dao.FC_RESULT_QUERY_DAO_ERROR_MESSAGE, "User"));
			}
		} catch (Exception e) {
			result.setResultCode(Constants.Dao.FC_RESULT_QUERY_DAO_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Dao.FC_RESULT_QUERY_DAO_ERROR_MESSAGE, "User"));
			log.error("queryUserById error## "+ e.getMessage());
		}
		return result;
	}
	
	/**
	 * 根据申请人获取流程审批人员列表
	 * @param user
	 * @author wisdying
	 * @return
	 */
	public Result queryApproves(User user){
		Result result = new Result();
		try {
			List<User> approves = (List<User>)queryForList("User.selApproves",user);
			if(user!=null){
				result.setResultCode(Constants.FC_RESULT_SUCCESS_CODE);
				result.setResultMessage(Constants.FC_RESULT_SUCCESS_MESSAGE);
				result.setResultValue(approves);
			}else{
				result.setResultCode(Constants.Dao.FC_RESULT_QUERY_DAO_ERROR_CODE);
				result.setResultMessage(String.format(Constants.Dao.FC_RESULT_QUERY_DAO_ERROR_MESSAGE, "User"));
			}
		} catch (Exception e) {
			result.setResultCode(Constants.Dao.FC_RESULT_QUERY_DAO_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Dao.FC_RESULT_QUERY_DAO_ERROR_MESSAGE, "User"));
			log.error("queryApproves error## "+ e.getMessage());
		}
		return result;
	}
}
